package problem173;

import java.util.ArrayList;

//173. 二叉搜索树迭代器
//https://leetcode.cn/problems/binary-search-tree-iterator/?envType=study-plan-v2&envId=top-interview-150

class BSTIterator {
    ArrayList<TreeNode> list;
    int index;

    public BSTIterator(TreeNode root) {
        list = new ArrayList<>();
        index = 0;
        initList(root);
    }
    
    public int next() {
        int ret = list.get(index).val;
        index++;
        return ret;
    }
    
    public boolean hasNext() {
        return index < list.size();
    }

    private void initList(TreeNode root) {
        if(root == null) return;
        initList(root.left);
        list.add(root);
        initList(root.right);
    }
}
